package com.atguigu.srb.core.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.srb.core.listener.ExcelDictListener;
import com.atguigu.srb.core.mapper.DictMapper;
import com.atguigu.srb.core.pojo.dto.ExcelDictDTO;
import com.atguigu.srb.core.pojo.entity.Dict;
import com.atguigu.srb.core.service.DictService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author Mr.bilal
 * @since 2023-07-18
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public void importDictExcel(MultipartFile multipartFile) {
        try {
            EasyExcel.read(multipartFile.getInputStream(),
                    ExcelDictDTO.class,
                    new ExcelDictListener(baseMapper))
                    .sheet("数据字典").doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<ExcelDictDTO> exportDictExcel() {

        List<Dict> dictS = baseMapper.selectList(null);
        List<ExcelDictDTO> excelDictDTOS = dictS.stream().map(dict ->{
            ExcelDictDTO excelDictDTO = new ExcelDictDTO();
            BeanUtils.copyProperties(dict,excelDictDTO);
            return excelDictDTO;
        }).collect(Collectors.toList());

        return excelDictDTOS;
    }

    @Override
    public List<Dict> getDictListByParentId(Long parentId) {
        List<Dict> dictList = null;
        dictList = (List<Dict>) redisTemplate.opsForValue().get("srb:core:dictList:"+parentId);
        if (dictList == null) {
            dictList = getDictListByParentIdDb(parentId);
            if (dictList != null) {
                redisTemplate.opsForValue().set("srb:core:dictList:"+parentId,dictList);
            }else {
                redisTemplate.opsForValue().set("srb:core:dictList:"+parentId,new ArrayList<Dict>(),10, TimeUnit.SECONDS);
            }
        }
        return dictList;
    }

    @Override
    public List<Dict> getDictListByDictCode(String dictCode) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_code",dictCode);
        Dict dict = baseMapper.selectOne(queryWrapper);

        queryWrapper.clear();
        queryWrapper.eq("parent_id",dict.getId());
        List<Dict> dicts = baseMapper.selectList(queryWrapper);
        return dicts;
    }

    @Override
    public String getDictNameByDictCodeAndValueId(String dictCode, Integer valueId) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_code",dictCode);
        Dict dict = baseMapper.selectOne(queryWrapper);
        Long parentId = dict.getId();
        queryWrapper.clear();
        queryWrapper.eq("parent_id",parentId);
        queryWrapper.eq("value",valueId);
        Dict selected = baseMapper.selectOne(queryWrapper);
        if (selected == null){
            return "字典名称";
        }
        return selected.getName();
    }

    public List<Dict> getDictListByParentIdDb(Long parentId) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        List<Dict> dicts = baseMapper.selectList(queryWrapper);
        for (Dict dict : dicts) {
            queryWrapper.clear();
            queryWrapper.eq("parent_id",dict.getId());
            Integer integer = baseMapper.selectCount(queryWrapper);
            if (integer >0){
                dict.setHasChildren(true);
            }
        }
        return dicts;
    }
}
